package zhou.cutfruit;

import java.util.ArrayList;
import java.util.List;

import zhou.cutfruit.util.PreparedSoundSingle;
import zhou.cutfruit.view.GameView;
import android.app.Activity;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class GameViewActivity extends Activity {
	
	GameView gameView;//游戏界面
	public static final int pointCount = 4;
	public List<Point> points = new ArrayList<Point>();
	
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// 全屏显示
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		//无标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		gameView = new GameView(this);

		
		//为gameView设置手指touch事件
		gameView.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					//如果是down,调用单例播放挥刀声音
					PreparedSoundSingle.getInstance().playCutSound();
					break;
				case MotionEvent.ACTION_MOVE:
					//如果是move，判断是否切中
					gameView.judgeCutting((int) event.getX(),
							(int) event.getY());
					
					//将move产生的点连缀成路径，并绘制，形成刀锋效果，有待改进（一直move不up，栈溢出）
					Point point = new Point((int) event.getX(), (int) event
							.getY());
					if (points.size() > pointCount) {
						Path path = new Path();
						path.moveTo(points.get(0).x, points.get(0).y);

						for (Point point2 : points) {

							path.lineTo(point2.x, point2.y);
						}
						path.close();

						gameView.setPath(path);

						points = points.subList(pointCount / 2, pointCount);

					}
					points.add(point);
					if (points.size() > 40) {
						points.clear();
						Log.i("clear", "motion_move的时候clear了");
					}

					break;
				case MotionEvent.ACTION_UP:
					Path path = new Path();
					gameView.setPath(path);
					points.clear();//清空points，否则栈溢出
					Log.i("clear", "motion_up的时候clear了");
					break;
				default:
					break;
				}
				return true;
			}
		});
		
		
		setContentView(gameView);

	}

}
